using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Role : MonoBehaviour
{
    /// <summary>
    /// 生命值
    /// </summary>
    private int healthPoint = 10;
    /// <summary>
    /// 生命值上限
    /// </summary>
    private int healthPointMax = 10;
    private Dictionary<string,int> attribute = new Dictionary<string, int>();
    private int speed = 2;
    private Animator animator = null;
    private SPUM_Prefabs spum = null;
    private SPUM_SpriteList spriteList = null;
    public HitBox hitBox;
    private CapsuleCollider2D capsuleCollider2D;
    // Start is called before the first frame update
    void Start()
    {
        spum = gameObject.GetComponent<SPUM_Prefabs>();
        animator = spum._anim;
        spriteList = spum._spriteOBj;
        capsuleCollider2D = gameObject.GetComponent<CapsuleCollider2D>();

        //属性初始化
        attribute["攻击力"] = 1;

    }

    // Update is called once per frame
    void Update()
    {

    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "Item")
        {
            ItemObj item = collision.gameObject.GetComponent<ItemObj>();
            item.BeGet(this);
        }
    }

    public void Attack()
    {
        animator.SetTrigger("Attack");
        ActiveHitBox();
    }

    /// <summary>
    /// 对该Role造成伤害，如果造成复伤害，则为生命回复
    /// </summary>
    /// <param name="value">伤害值</param>
    public void Damage(int value)
    {
        ChangeHealthPoint(value);
        Debug.Log("受伤害:"+value);
        if(healthPoint > 0)
        {
            animator.SetTrigger("Damage");
        }
        else{
            animator.SetTrigger("Die");
            capsuleCollider2D.enabled = false;
        }
    }

    /// <summary>
    /// 直接设置玩家生命值
    /// </summary>
    /// <param name="value"></param>
    private void SetHealthPoint(int value)
    {
        healthPoint = value;
        if(healthPoint > healthPointMax)
        {
            healthPoint = healthPointMax;
        }else if(healthPoint < 0)
        {
            healthPoint = 0;
        }
    }

    /// <summary>
    /// 改变生命值
    /// </summary>
    /// <param name="value"></param>
    private void ChangeHealthPoint(int value)
    {
        healthPoint += value;
        if(healthPoint > healthPointMax)
        {
            healthPoint = healthPointMax;
        }else if(healthPoint < 0)
        {
            healthPoint = 0;
        }
    }

    /// <summary>
    /// 激活伤害判定区
    /// </summary>
    public void ActiveHitBox()
    {
        int atk = attribute["攻击力"];
        hitBox.Attack(atk);
    }

    /// <summary>
    /// 获取指定属性的值
    /// </summary>
    /// <param name="attrName">属性名称</param>
    public int GetAttrValue(string attrName) 
    {
        return attribute[attrName];
    }

    /// <summary>
    /// 设定指定属性的值
    /// </summary>
    /// <param name="attrName">属性名称</param>
    /// <param name="value">目标值</param>
    public void SetAttrValue(string attrName,int value) 
    {
        if (!attribute.ContainsKey(attrName)) 
        {
            Debug.LogError("目标属性["+ attrName + "]不存在");
            return;
        }
        attribute[attrName] = value;
    }

    public void Jump() 
    {
    
    }
}
